package com.sx.gongzhong.activity;

import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;

import com.sx.gongzhong.BaseActivity;
import com.sx.gongzhong.R;
import com.sx.gongzhong.net.ServiceFactory;
import com.sx.gongzhong.utils.BaseUtil;
import com.sx.gongzhong.widget.LoadDialog;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by admin on 2018/1/3.
 */

public class WebActivity extends BaseActivity {
    @BindView(R.id.frameLayout)
    FrameLayout frameLayout;
    private WebView webView;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_web);
        ButterKnife.bind(this);
        intiWebView();
        int from = getIntent().getIntExtra("from", 1);
        String title = getIntent().getStringExtra("title");
        if (!BaseUtil.isEmpty(title)) {//轮播图
            setTitleName(title);
            webView.loadUrl(getIntent().getStringExtra("articleUrl"));
        } else {
            if (from == 1) {
                setTitleName("便民公告详情");
            } else if (from == 2) {
                setTitleName("办事指南详情");
            } else if (from == 3) {
                setTitleName("平安宣传详情");
            }
            webView.loadUrl(ServiceFactory.getUrl(getIntent().getStringExtra("detailUrl")));
        }
    }

    private void intiWebView() {
        webView = new WebView(getApplicationContext());
        frameLayout.addView(webView);
        webView.setVerticalScrollBarEnabled(false);
        webView.setHorizontalScrollBarEnabled(false);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                if (!webView.canGoBack()){
                    LoadDialog.show(WebActivity.this,"加载中...");
                }
            }
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                LoadDialog.dismiss(WebActivity.this);
                if (!webView.getSettings().getLoadsImagesAutomatically()) {
                    webView.getSettings().setLoadsImagesAutomatically(true);
                }
            }
            @Override
            public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
                super.onReceivedHttpError(view, request, errorResponse);
                LoadDialog.dismiss(WebActivity.this);
            }
        });
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                return super.onJsAlert(view, url, message, result);
            }
        });
        WebSettings webSettings = webView.getSettings();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setBlockNetworkImage(false);
        webSettings.setDomStorageEnabled(true);
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
                webView.goBack();
                return true;
            } else {
                finish();
            }
        }
        return false;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            if (webView.canGoBack()){
                webView.goBack();
            }else {
                finish();
                return super.onOptionsItemSelected(item);
            }
        }
        return false;
    }

    @Override
    protected void onDestroy() {
        if (webView!=null){
            frameLayout.removeAllViews();
            webView.removeAllViews();
            webView.destroy();
        }
        super.onDestroy();
    }
}
